package cn.com.assistant.service.impl;

import cn.com.assistant.entity.User;
import cn.com.assistant.mapper.UserMapper;
import cn.com.assistant.model.UserPOJO;
import cn.com.assistant.service.UserService;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.TokenUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    public UserMapper userMapper;
 
    @Override
    public List<User> queryAllPubUser() {
        return userMapper.queryAllUser();
    }

    @Override
    public ResponseMessage userAdd(UserPOJO userPOJO) {
        // 对象数据转换
        User user = new User();
        BeanUtils.copyProperties(userPOJO, user);
        user.setRole(1);
        user.setStatus(1);
        user.setPassword("111111");
        user.setCreatedDate(new Date());
        int result = userMapper.insert(user);
        if (result == 1) {
            return ResponseMessage.defaultResponse().setMsg("添加成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("添加失败！");
        }
    }

    @Override
    public ResponseMessage userLogin(String username, String password, HttpSession session) {
        User entity = userMapper.findByUsername(username);
        if (entity != null && password.equals(entity.getPassword())) {
            // 返回前端时密码置空
            entity.setPassword("");
            Map<String, Object> map = new HashMap();
            map.put("userInfo", entity);
            String token = TokenUtil.getAdminToken(entity);
            map.put("token", token);
            session.setAttribute(username + "token", token);
            //
            session.setMaxInactiveInterval(120*60);
            return ResponseMessage.defaultResponse().setMsg("登录成功！").setData(map);
        } else {
            return ResponseMessage.failResponse().setMsg("登录失败！");
        }
    }

    @Override
    public ResponseMessage noToken() {
        return ResponseMessage.failResponse().setMsg("无token，请重新登录！");
    }

    @Override
    public ResponseMessage noUser() {
        return ResponseMessage.failResponse().setMsg("用户不存在，请重新登录！");
    }

    @Override
    public ResponseMessage expireToken() {
        return ResponseMessage.failResponse().setMsg("token已过期！");
    }

    @Override
    public ResponseMessage illegalToken() {
        return ResponseMessage.failResponse().setMsg("token不合法！");
    }

    @Override
    public ResponseMessage findByToken(String token) {
        Integer adminUserId = TokenUtil.getAdminUserId(token);
        User adminUser = userMapper.findById(adminUserId);
        return ResponseMessage.defaultResponse().setData(adminUser);
    }

    @Override
    public ResponseMessage existByUsername(String username, Integer id) {
        ResponseMessage responseMessage = ResponseMessage.defaultResponse();
        List<User> userEntities = userMapper.findByUsernameAndIdNot(username, id);
        if (null != userEntities && userEntities.size() > 0) {
            responseMessage.setStatus(0).setMsg("该账号已经存在!");
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage findByPhoneAndIdNot(String phone, Integer id) {
        ResponseMessage responseMessage = ResponseMessage.defaultResponse();
        List<User> userEntities = userMapper.findByPhoneAndIdNot(phone, id);
        if (null != userEntities && userEntities.size() > 0) {
            responseMessage.setStatus(0).setMsg("该手机号码已经绑定系统账号，请重新输入!");
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage userDelete(Integer id) {
        int result = userMapper.userDelete(id);
        if (result == 1) {
            return ResponseMessage.defaultResponse().setMsg("删除成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("删除失败！");
        }
    }

    @Override
    public ResponseMessage findByUserName(String username) {
        User entity = userMapper.findByUsername(username);
        if (null != entity) {
            // 返回前端时密码置空
            entity.setPassword("");
            return ResponseMessage.defaultResponse().setMsg("查询成功！").setData(entity);
        } else {
            return ResponseMessage.failResponse().setMsg("查询失败");
        }
    }

    @Override
    public User findById(Integer id) {
        User entity = userMapper.findById(id);
            // 返回前端时密码置空
        if (null != entity) {
            entity.setPassword("");
        }
        return entity;
    }

    @Override
    public ResponseMessage userUpdate(UserPOJO userPOJO) {
        // 对象数据转换
        User user = new User();
        BeanUtils.copyProperties(userPOJO, user);
        // 修改修改日期
        user.setUpdatedDate(new Date());
        int result = userMapper.userUpdate(user);
        if (result > 0) {
            return ResponseMessage.defaultResponse().setMsg("更新成功！").setData(result);
        } else {
            return ResponseMessage.defaultResponse().setMsg("更新失败！");
        }
    }

    @Override
    public ResponseMessage userPasswordReset(Integer id) {
        User user = userMapper.findById(id);
        user.setUpdatedDate(new Date());
        user.setPassword("111111");
        int result = userMapper.userUpdate(user);
        if (result > 0) {
            return ResponseMessage.defaultResponse().setMsg("重置密码成功！").setData(result);
        } else {
            return ResponseMessage.defaultResponse().setMsg("重置密码失败！");
        }
    }

    @Override
    public ResponseMessage userStatusReset(Integer id, int status) {
        User user = userMapper.findById(id);
        // 修改修改日期
        user.setUpdatedDate(new Date());
        // 修改状态
        user.setStatus(status);
        int result = userMapper.userUpdate(user);
        if (result > 0) {
            return ResponseMessage.defaultResponse().setMsg("状态修改成功！").setData(result);
        } else {
            return ResponseMessage.defaultResponse().setMsg("转态修改失败！") ;
        }
    }

    @Override
    public ResponseMessage findByPage(Integer page, Integer size, Map<String, Object> filter) {
//        PageHelper.startPage(page, size);
//        List<User> userEntities = userMapper.findByPage(filter);
//        PageInfo<User> pageInfo = new PageInfo<>(userEntities);
//        //用PageInfo包装了一下（官方推荐）
        return ResponseMessage.defaultResponse();

    }

}